﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;

namespace FileTranslator
{
    public abstract class CsvFileLoader : Loader
    {
        protected override void BeforeLoadWork()
        {
            // 如果之前已经有打开了的文件，先关闭它
            this.Close();
            if (VerifyFile() == false)
            {
                // todo: log down
                throw new FileNotFoundException();
            }
        }

        protected override void AfterLoadWork()
        {
        }

        private bool VerifyFile()
        {
            return File.Exists(TargetPath) && Path.GetExtension(TargetPath) == ".csv";
        }

        protected override void DoLoad()
        {
            try
            {
                //CSV文件中间数据
                csvTable = new DataTable();

                CsvHelper.CsvToLocalTbl(TargetPath, ref csvTable);

                DoGroupBy();

                DataVessel.DataTable = csvTable;

                // TODO: log down
            }
            catch (System.Exception ex)
            {
                // TODO: log down exceptions occurs in loading csv files
                throw ex;
            }
        }

        protected override void DoGroupBy()
        {
            if (DataVessel.GroupByHint != null)
            {
                List<string> keys = new List<string>();

                // 根据GroupBy进行分组，方法是将GroupBy所需要的列中内容进行拼接，如果没有出现过就说明是新组
                foreach (DataRow rd in csvTable.Rows)
                {
                    string key = "";
                    for (int i = 0; i < DataVessel.GroupByHint.Count(); i++)
                    {
                        key += (rd[DataVessel.GroupByHint[i]].ToString() + " ");
                    }

                    if (keys.Contains(key) == false)
                    {
                        // 第一个元素
                        keys.Add(key);
                    }
                    else
                    {
                        // 已经出现过了，什么都不做
                    }
                }

                // 生成必要的DataView
                foreach (string key in keys)
                {
                    string[] values = key.Split(new char[1] { ' ' });
                    string rowFilter = "";
                    for (int i = 0; i < values.Length - 1; i++)
                    {
                        rowFilter += DataVessel.GroupByHint[i] + "= '" + values[i] + "' AND ";
                    }
                    rowFilter += "'1'='1'";

                    DataView dv = new DataView(csvTable);
                    dv.RowFilter = rowFilter;
                    DataVessel.CreateNewDataTableWrapper(dv);
                }
            }

            // 一定会有一个整张表的DataView放在0号位置
            DataView dvwhole = new DataView(csvTable);
            DataVessel.CreateWholeTableWrapper(dvwhole, DataVessel.GetDataTableWrappers());   
        }
    }

    public class FmtFileLoader : CsvFileLoader
    {
        public FmtFileLoader()
        {
            DataVessel = new FmtContainer();
        }
    }

    public class FldFileLoader : CsvFileLoader
    {
        public FldFileLoader()
        {
            DataVessel = new FldContainer();   
        }
    }

    public class FldProcessFileLoader : CsvFileLoader
    {
        public FldProcessFileLoader()
        {
            DataVessel = new FldProcessContainer();
        }
    }

    public class ElemBussIdFileLoader : CsvFileLoader
    {
        public ElemBussIdFileLoader()
        {
            DataVessel = new ElemBussIdContainer();
        }
    }

    public class SourFldTpFileLoader : CsvFileLoader
    {
        public SourFldTpFileLoader()
        {
            DataVessel = new SourFldTpContainer();
        }
    }
}
